<?php
/**
 * 语音播报系统 - 场景测试程序
 * 模拟实际的语音播报场景
 */

error_reporting(E_ALL);
ini_set('display_errors', 'on');

// 颜色输出类
class ConsoleColor {
    const RESET = "\033[0m";
    const RED = "\033[31m";
    const GREEN = "\033[32m";
    const YELLOW = "\033[33m";
    const BLUE = "\033[34m";
    const CYAN = "\033[36m";
    const MAGENTA = "\033[35m";

    public static function success($text) {
        return self::GREEN . "✅ " . $text . self::RESET;
    }

    public static function error($text) {
        return self::RED . "❌ " . $text . self::RESET;
    }

    public static function warning($text) {
        return self::YELLOW . "⚠️ " . $text . self::RESET;
    }

    public static function info($text) {
        return self::BLUE . "ℹ️ " . $text . self::RESET;
    }

    public static function title($text) {
        return self::CYAN . "🎤 " . $text . self::RESET;
    }

    public static function scenario($text) {
        return self::MAGENTA . "📍 " . $text . self::RESET;
    }
}

// 场景测试类
class VoiceScenarioTest {
    private $scenarios = [];
    private $passCount = 0;
    private $totalCount = 0;

    public function run() {
        echo "\n";
        echo ConsoleColor::title("语音播报系统 - 场景测试") . "\n";
        echo str_repeat("=", 70) . "\n\n";

        // 场景 1: 签到开始
        $this->testCheckInStart();

        // 场景 2: 签到结束
        $this->testCheckInEnd();

        // 场景 3: 重复刷脸
        $this->testDuplicateFace();

        // 场景 4: 多个会员连续签到
        $this->testMultipleMembersCheckIn();

        // 场景 5: 特殊字符处理
        $this->testSpecialCharacters();

        // 输出结果
        $this->printResults();
    }

    private function testCheckInStart() {
        echo ConsoleColor::scenario("场景 1: 签到开始") . "\n";
        echo str_repeat("-", 70) . "\n";

        $testCases = [
            [
                'name' => '李四',
                'card_no' => '1542415',
                'expected' => '李四，签到成功',
            ],
            [
                'name' => '张三',
                'card_no' => '1542416',
                'expected' => '张三，签到成功',
            ],
            [
                'name' => '王五',
                'card_no' => '1542417',
                'expected' => '王五，签到成功',
            ],
        ];

        foreach ($testCases as $case) {
            $this->totalCount++;
            $voiceMessage = "{$case['name']}，签到成功";
            
            if ($voiceMessage === $case['expected']) {
                echo ConsoleColor::success("会员 {$case['name']} 签到开始语音正确") . "\n";
                echo "  📢 播报文字: \"$voiceMessage\"\n";
                $this->passCount++;
            } else {
                echo ConsoleColor::error("会员 {$case['name']} 签到开始语音错误") . "\n";
                echo "  期望: \"{$case['expected']}\"\n";
                echo "  实际: \"$voiceMessage\"\n";
            }
        }
        echo "\n";
    }

    private function testCheckInEnd() {
        echo ConsoleColor::scenario("场景 2: 签到结束") . "\n";
        echo str_repeat("-", 70) . "\n";

        $testCases = [
            [
                'name' => '李四',
                'points' => 6,
                'duration' => 30,
                'expected' => '李四，签到结束，本次获得6积分',
            ],
            [
                'name' => '张三',
                'points' => 8,
                'duration' => 40,
                'expected' => '张三，签到结束，本次获得8积分',
            ],
            [
                'name' => '王五',
                'points' => 10,
                'duration' => 50,
                'expected' => '王五，签到结束，本次获得10积分',
            ],
        ];

        foreach ($testCases as $case) {
            $this->totalCount++;
            $voiceMessage = "{$case['name']}，签到结束，本次获得{$case['points']}积分";
            
            if ($voiceMessage === $case['expected']) {
                echo ConsoleColor::success("会员 {$case['name']} 签到结束语音正确") . "\n";
                echo "  📢 播报文字: \"$voiceMessage\"\n";
                echo "  ⏱️  时长: {$case['duration']} 分钟 | 💰 积分: +{$case['points']}\n";
                $this->passCount++;
            } else {
                echo ConsoleColor::error("会员 {$case['name']} 签到结束语音错误") . "\n";
                echo "  期望: \"{$case['expected']}\"\n";
                echo "  实际: \"$voiceMessage\"\n";
            }
        }
        echo "\n";
    }

    private function testDuplicateFace() {
        echo ConsoleColor::scenario("场景 3: 重复刷脸") . "\n";
        echo str_repeat("-", 70) . "\n";

        $testCases = [
            [
                'name' => '李四',
                'interval' => 5,
                'expected' => '请勿重复刷脸',
            ],
            [
                'name' => '张三',
                'interval' => 8,
                'expected' => '请勿重复刷脸',
            ],
        ];

        foreach ($testCases as $case) {
            $this->totalCount++;
            $voiceMessage = '请勿重复刷脸';
            
            if ($voiceMessage === $case['expected']) {
                echo ConsoleColor::success("会员 {$case['name']} 重复刷脸提示正确") . "\n";
                echo "  📢 播报文字: \"$voiceMessage\"\n";
                echo "  ⏱️  间隔: {$case['interval']} 秒\n";
                $this->passCount++;
            } else {
                echo ConsoleColor::error("会员 {$case['name']} 重复刷脸提示错误") . "\n";
            }
        }
        echo "\n";
    }

    private function testMultipleMembersCheckIn() {
        echo ConsoleColor::scenario("场景 4: 多个会员连续签到") . "\n";
        echo str_repeat("-", 70) . "\n";

        $members = [
            ['name' => '李四', 'card_no' => '1542415'],
            ['name' => '张三', 'card_no' => '1542416'],
            ['name' => '王五', 'card_no' => '1542417'],
            ['name' => '赵六', 'card_no' => '1542418'],
        ];

        echo "模拟 " . count($members) . " 个会员连续签到:\n";
        
        foreach ($members as $member) {
            $this->totalCount++;
            $voiceMessage = "{$member['name']}，签到成功";
            echo ConsoleColor::success("会员 {$member['name']} 签到") . "\n";
            echo "  📢 播报文字: \"$voiceMessage\"\n";
            echo "  🆔 卡号: {$member['card_no']}\n";
            $this->passCount++;
        }
        echo "\n";
    }

    private function testSpecialCharacters() {
        echo ConsoleColor::scenario("场景 5: 特殊字符处理") . "\n";
        echo str_repeat("-", 70) . "\n";

        $testCases = [
            [
                'name' => '李四-VIP',
                'expected' => '李四-VIP，签到成功',
            ],
            [
                'name' => '张三（管理员）',
                'expected' => '张三（管理员），签到成功',
            ],
            [
                'name' => '王五·高级会员',
                'expected' => '王五·高级会员，签到成功',
            ],
        ];

        foreach ($testCases as $case) {
            $this->totalCount++;
            $voiceMessage = "{$case['name']}，签到成功";
            
            if ($voiceMessage === $case['expected']) {
                echo ConsoleColor::success("特殊字符处理正确: {$case['name']}") . "\n";
                echo "  📢 播报文字: \"$voiceMessage\"\n";
                $this->passCount++;
            } else {
                echo ConsoleColor::error("特殊字符处理错误: {$case['name']}") . "\n";
            }
        }
        echo "\n";
    }

    private function printResults() {
        echo str_repeat("=", 70) . "\n";
        echo ConsoleColor::title("场景测试结果") . "\n";
        echo str_repeat("=", 70) . "\n\n";

        $passPercentage = ($this->passCount / $this->totalCount) * 100;

        echo "总场景数: " . ConsoleColor::CYAN . $this->totalCount . ConsoleColor::RESET . "\n";
        echo "通过数: " . ConsoleColor::GREEN . $this->passCount . ConsoleColor::RESET . "\n";
        echo "失败数: " . ConsoleColor::RED . ($this->totalCount - $this->passCount) . ConsoleColor::RESET . "\n";
        echo "通过率: " . ConsoleColor::CYAN . sprintf("%.1f%%", $passPercentage) . ConsoleColor::RESET . "\n\n";

        if ($passPercentage == 100) {
            echo ConsoleColor::success("所有场景测试通过！✨") . "\n";
        } elseif ($passPercentage >= 80) {
            echo ConsoleColor::warning("大部分场景通过，请检查失败项") . "\n";
        } else {
            echo ConsoleColor::error("场景测试失败率较高") . "\n";
        }

        echo "\n";
        echo str_repeat("=", 70) . "\n";
        echo ConsoleColor::info("功能测试建议:") . "\n";
        echo str_repeat("-", 70) . "\n";
        echo "1. 启动 TCP 服务器\n";
        echo "   php tools/device_tcp/simple_server.php\n\n";
        echo "2. 启动 WebSocket 服务器\n";
        echo "   php websocket_server.php start\n\n";
        echo "3. 打开浏览器\n";
        echo "   http://localhost:8000/voice_player.html\n\n";
        echo "4. 进行以下测试:\n";
        echo "   ✓ 点击 '🔊 测试语音' 按钮\n";
        echo "   ✓ 使用设备进行人脸识别签到\n";
        echo "   ✓ 验证语音播报内容\n";
        echo "   ✓ 调整音量、语速、音调\n";
        echo "   ✓ 查看浏览器控制台日志\n\n";
        echo "5. 检查日志文件\n";
        echo "   tail -f tools/device_tcp/logs/YYYY-MM-DD.log\n";
        echo "\n";
    }
}

// 运行场景测试
$test = new VoiceScenarioTest();
$test->run();
?>

